﻿using System;
using System.Web;
using InfoJetSoft.Service.Entity;

namespace InfoJetSoft.Service.Util
{
	
	public class PathUtil
	{
		
		public static string formularPath(string path)
		{
			if (!path.StartsWith("/"))
			{
				return "//" + path;
			}
			else
			{
				return path;
			}
		}

        //CID 0618 //2010.07.05 //Begin
        public static bool isResourceFileEnabled(Manifest manifest)
        {
            if (ConfigUtil.ResourceFilePath() == null && manifest.JetContext.ResourceFileStorage == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        //CID 0618 //2010.07.05 //End

		//将src中可能的http://site 头去掉,只保留绝对路径.
		public static string getAbsolutePath( string src )
		{
			try
			{
				//Uri不能接受/AnotherVirtualPath/guid/pic.gif这种构造参数.
				//所以在这里使用Uri和try将src转换为AbsolutePath,如果它不是.
				Uri uri = new Uri( src );
				//CID 0148 //2008.11.12 //Begin
				//在DotNet 1.1中,AbsolutePath包含Query,但是2.0中,不包含Query.应该使用PathAndQuery.
				//MSDN: PathAndQuery, A String that contains the AbsolutePath and Query properties separated by a question mark (?).
				//这不是一个.NET版本兼容性问题,因为直接使用了PathAndQuery.
				src = uri.PathAndQuery;
				//CID 0148 //2008.11.12 //End
			}
			catch( Exception e )
			{
				//如果有错误,可能src是/AnotherVirtualPath/guid/pic.gif,忽略.
			}
			return src;
		}

		public static string getAppPath( HttpContext httpContext )
		{
			string appPath = httpContext.Request.ApplicationPath;
			//CID 0549 //2010.05.20 //Begin
			if( appPath == null )
			{
				appPath = "";
			}
			//CID 0549 //2010.05.20 //End
			if( appPath.Equals( "/" ) )
			{
				appPath = "";
			}
			return appPath;
		}

		//CID 0139 //2008.10.09 //Begin
		public static string getBasePath( HttpContext httpContext )
		{
			string basePath = httpContext.Request.Url.Scheme + "://" + httpContext.Request.Url.Authority + getAppPath( httpContext );
			return basePath;
		}
		//CID 0139 //2008.10.09 //End

		//CID 0119 //2008.09.20 //Begin
		public static bool isLocalPath( HttpContext context, string url )
		{
			string port = context.Request.ServerVariables["SERVER_PORT"];
			string protocol = context.Request.ServerVariables["SERVER_PROTOCOL"].ToLower();
			string server = context.Request.ServerVariables["SERVER_NAME"];
			string appPath = context.Request.ApplicationPath;

			string appFullPath = null;
			string localFullPath = null;
	
			if( context.Request.ServerVariables["HTTPS"].ToLower().Equals( "on" ) )
			{
				if( port.Equals( "443" ) )
				{
					appFullPath = "https://" + server + appPath;
					localFullPath = "https://localhost" + appPath;
				}
				else
				{
					appFullPath = "https://" + server + ":" + port + appPath;
					localFullPath = "https://localhost:" + port + appPath;
				}
			}
			else
			{
				if( port.Equals( "80" ) )
				{
					appFullPath = "http://" + server + appPath;
					localFullPath = "http://localhost" + appPath;
				}
				else
				{
					appFullPath = "http://" + server + ":" + port + appPath;
					localFullPath = "http://localhost:" + port + appPath;
				}
			}

			appFullPath = appFullPath.ToLower();
			localFullPath = localFullPath.ToLower();

			url = url.ToLower();

			if( url.StartsWith( appFullPath ) || url.StartsWith( localFullPath ) )
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		//CID 0119 //2008.09.20 //End
	}
}